package com.dgut.medicalsystem.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.medicalsystem.entity.SysPermision;
import com.dgut.medicalsystem.entity.SysRolePermision;
import com.dgut.medicalsystem.entity.dto.RolePermissionInfo;
import com.dgut.medicalsystem.service.SysPermisionService;
import com.dgut.medicalsystem.service.SysRolePermisionService;
import com.dgut.medicalsystem.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 权限表	 前端控制器
 * </p>
 *
 * @author 坚宝医疗
 * @since 2021-09-03
 */
@RestController
@RequestMapping("/sys-permision")
public class SysPermisionController {

    @Autowired
    SysPermisionService sysPermisionService;
    @Autowired
    SysRolePermisionService sysRolePermisionService;
    @Autowired
    SysRoleService sysRoleService;

    @SaCheckLogin
    @SaCheckRole("superadmin")
    @RequestMapping("check")
    public Map<String, Object> checkPermissions() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", sysPermisionService.list());
        return map;
    }

    @SaCheckLogin
    @SaCheckRole("superadmin")
    @RequestMapping("addPermissions")
    public Map<String, Object> addPermissions(@RequestBody SysPermision sysPermision) {
        Map<String, Object> map = new HashMap<>();
        if (sysPermisionService.save(sysPermision)) {
            map.put("code", "200");
            map.put("info", "插入成功");
        } else {
            map.put("code", "400");
            map.put("info", "插入失败");
        }
        return map;
    }

    @SaCheckLogin
    @SaCheckRole("superadmin")
    @RequestMapping("addStaffPermissions")
    public Map<String, Object> addStaffPermissions(@RequestBody RolePermissionInfo rolePermissionInfo) {
        int roleId = rolePermissionInfo.getRoleId();
        String[] s = rolePermissionInfo.getDisplayNames();
        //权限id清单
        List<SysPermision> perIdList = sysPermisionService.list(new QueryWrapper<SysPermision>()
                .select("Id").in("display_name", Arrays.asList(s)));
        List<SysRolePermision> res_list = new ArrayList<>(perIdList.size());
        for (SysPermision t : perIdList) {
            SysRolePermision sysRolePermision = new SysRolePermision();
            sysRolePermision.setPermisionId(t.getId());
            sysRolePermision.setRoleId(roleId);
            res_list.add(sysRolePermision);
        }
        Map<String, Object> map = new HashMap<>();
        if (sysRolePermisionService.saveBatch(res_list)) {
            map.put("code", "200");
            map.put("info", "插入成功");
        } else {
            map.put("code", "400");
            map.put("info", "插入失败");
        }
        return map;
    }

    @SaCheckLogin
    @SaCheckRole("superadmin")
    @RequestMapping("checkStaffPermissions")
    public Map<String, Object> checkStaffPermissions(@RequestBody SysRolePermision sysRolePermision) {
        int roleId = sysRolePermision.getRoleId();
        //身份权限清单
        List<SysRolePermision> list = sysRolePermisionService.list(new QueryWrapper<SysRolePermision>().select("permision_id").eq("role_id", roleId));
        List<Integer> permissions_list = new ArrayList<>(list.size());
        for (SysRolePermision s : list) {
            permissions_list.add(s.getPermisionId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", sysPermisionService.list(new QueryWrapper<SysPermision>().in("id", permissions_list)));
        return map;
    }

    @SaCheckLogin
    @SaCheckRole("superadmin")
    @RequestMapping("checkRoles")
    public Map<String, Object> checkRoles() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", sysRoleService.list());
        return map;
    }

    @SaCheckLogin
    @SaCheckRole("superadmin")
    @RequestMapping("removePermissions")
    public Map<String, Object> removePermissions(@RequestBody RolePermissionInfo rolePermissionInfo) {
        int roleId = rolePermissionInfo.getRoleId();
        String[] s = rolePermissionInfo.getDisplayNames();
        //权限id清单
        List<SysPermision> perIdList = sysPermisionService.list(new QueryWrapper<SysPermision>()
                .select("Id").in("display_name", Arrays.asList(s)));
        List<Integer> IDlist = new ArrayList<>(perIdList.size());
        for (SysPermision t : perIdList) {
            IDlist.add(t.getId());
        }
        Map<String, Object> map = new HashMap<>();
        if (sysRolePermisionService.remove(new QueryWrapper<SysRolePermision>()
                .eq("role_id", roleId)
                .in("permision_id", IDlist))) {
            map.put("code", "200");
            map.put("info", "删除成功");
        } else {
            map.put("code", "400");
            map.put("info", "删除失败");
        }
        return map;
    }
}
